<template>
  <div class="search-content">
    <SearchHeader hide-btn>
      <SearchInput
        slot="extra"
        ref="search"
        :search-value.sync="searchText"
        @clearKey="clearKey"
        @cancel="handleCancelClick"
        placeholder="搜索"
        transparent
        disable-fouce
      />
    </SearchHeader>
    <div
      v-if="searchText"
      class="search-result"
    >
      <div
        v-if="isShow"
        class="search-dialog"
      >
        <div
          class="person-list"
        >
          <div class="result-all">
            <div v-show="searchData.length">
              <div
                :key="index"
                v-for="(item, index) in searchData"
                @tap="clickSearch(item)"
                class="search-list"
              >
                <div
                  v-html="item.photo"
                  class="name-circle"
                />
                <p
                  v-html="item.names"
                  class="item-name"
                />
                <span
                  v-if="item.checked"
                  class="item-checked"
                >已选择</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div
        v-if="!isShow"
        class="noData"
      >
        暂无搜索内容
      </div>
    </div>
  </div>
</template>
<script>

import searchMatch from 'pinyin-match';
import SearchHeader from '@/components/searchHeader';
import SearchInput from '@/components/searchInput';
import Organ from '../../class/Organ';

export default {
  name: 'SearchPage',
  components: {
    SearchHeader, SearchInput,
  },
  data() {
    return {
      searchText: '',
      isShow: false,
      searchList: [], // 搜索单人大池子
      newGroup: [],
      searchData: [],
      companyData: [],
    };
  },
  computed: {
    getZhid() {
      return JE.getCurrentUser().zhId;
    },
    userId() {
      return JE.getCurrentUser().id;
    },
  },
  watch: {
    searchText(newVal) {
      if (JE.isNotEmpty(newVal)) {
        this.searchText = newVal;
        this.isShow = true;
        if (newVal.trim()) {
          this.searchData = this.setSelected(this.getOwerPerson());
          if (this.searchData.length == 0) {
            this.isShow = false;
          }
        }
      } else {
        this.isShow = false;
        this.searchText = '';
      }
    },
  },
  created() {
    this.searchText = '';
  },
  async mounted() {
    this.searchText = '';
    plus.nativeUI.closeWaiting();
    const currentWeb = plus.webview.currentWebview();
    this.selectedData = currentWeb.selectedData;
    // this.getTree();
    this.searchList = await Organ.fetchGetAllPeoples(this.getZhid);
    // 获取所有的人员
    // this.getPeople();
    mui.later(() => {
      this.$refs.search.showSoftInput();
    }, 300);
  },
  methods: {
    // 处理传值选中
    setSelected(data) {
      if (this.selectedData.length) {
        data.map((dataItem) => {
          this.selectedData.map((selectedItem) => {
            if (selectedItem.id == dataItem.id) {
              dataItem.checked = true;
            }
          });
        });
        return data;
      }
      return data;
    },
    getOwerPerson() {
      if (!this.isShow) {
        return false;
      }
      // 如果查询的包含'.'就让为查询失败
      if (this.searchText.includes('.')) {
        this.isShow = true;
        return [];
      }
      const singArr = [];
      this.searchList.map((item) => {
        const searchArray = searchMatch.match(item.name, this.searchText);
        if (searchArray) {
          const name = item.name.replace(item.name.substr(searchArray[0], this.searchText.length), `<span style='color:#546EFE;'>${item.name.substr(searchArray[0], this.searchText.length)}</span>`);
          this.$set(item, 'names', name);
          singArr.push(item);
        }
      });
      return singArr;
    },
    // 监听 清除按钮事件
    clearKey() {
      this.searchText = '';
    },
    handleCancelClick() {
      mui.back(() => {
        this.searchText = '';
      });
    },
    clickItem(item) {
      this.activeIndex = item.id;
    },
    // 获取所有人员，按姓氏排列的所有人员
    async getPeople() {
      this.searchList = await Organ.fetchGetAllPeoples(this.getZhid);
    },
    // async getTree() {
    //   const list = await Organ.getUserInfoByUserId(this.userId);
    //   if (list.length) {
    //     // data为需要修改的tree，为点击组织树做处理
    //     this.getTreeNew(list);
    //   }
    // },
    // // 递归获取所有的人员，并且增加字段拼音,前端搜索数据来源
    // getTreeNew(treeList) {
    //   treeList.map((item) => {
    //     if (item.children.length) {
    //       this.getTreeNew(item.children);
    //     } else {
    //       if (item.type && item.type === '1') {
    //         this.$set(item, 'photo', JE.getUserPhoto(item.id, true));
    //         this.searchList.push(item);
    //       }
    //       return this.searchList;
    //     }
    //   });
    // },
    // 鼠标点击事件
    async clickSearch(item) {
      // 组装传参给company页面数据
      if (!item.checked) {
        this.companyData.push(item);
        this.backCompany();
      } else {
        JE.msg(`${item.name}已在您的人员选择中`);
      }
    },
    backCompany() {
      if (this.companyData.length) {
        const targetWebview = plus.webview.getWebviewById('_lanuch');
        mui.fire(targetWebview, 'creatGroupBack', { selection: this.companyData });
        // if (this.currentWebType == 'contact') {
        //   mui.fire(targetWebview, 'creatGroupBack', { selection: this.companyData });
        // } else {
        const selection = [];
        this.selectedData.map((selectedDataItem) => {
          selection.push(selectedDataItem);
        });
        selection.push(this.companyData[0]);
        mui.fire(targetWebview, 'creatGroupBackTree', { selection });
        // }
        mui.back();
      } else {
        this.searchText = '';
      }
    },
  },
};
</script>
<style>
  img{
    width: 80px!important;
    height: 80px!important;
    border-radius: 50%;
  }
  .noData{
    color:#666;
    text-align: center;
    line-height: 80px;
    font-size: 28px;
  }
</style>

<style lang="less" scoped>
.search-content{
  width: 100%;
  height: 100%;
  position: relative;
  background:#F8F8F8;
  overflow: hidden;
  .search-result{
    position: absolute;
    top: 80px;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    z-index: 1000;
    .search-condition{
      text-align: center;
      p{
        font-size:28px;
        font-weight:400;
        color:rgba(169,171,169,1);
        line-height:40px;
        text-align: center;
        padding-top: 60px;
        padding-bottom: 40px;
      }
      .search-type{
        font-weight:400;
        color:rgba(83,109,254,1);
        line-height:34px;
        span{
          font-size:24px;
          display: inline-block;
        }
        span:first-child{
          margin-right: 40px;
        }
        span:last-child{
          margin-left: 40px;
        }
      }
    }
    .search-dialog{
      width: 100%;
      height: 100%;
      overflow: hidden;
      position: relative;
      .search-title{
        width: 100%;
        overflow-x: scroll;
        border-bottom: 1px solid #F5F5F5;
        li{
          display: inline-block;
          width: 33%;
          text-align: center;
          height: 100px;
          span{
            font-size:32px;
            font-weight:400;
            color:rgba(63,63,63,1);
            display: inline-block;
            line-height: 96px;
          }
          .active-border{
            border-bottom: 2px solid #536DFE;
          }
        }
      }
      .title{
        width: 100%;
        padding: 20px 34px 0px 5px;
        font-size:28px;
        font-weight:500;
        color:rgba(190,190,190,1);
        line-height:40px;
        border-bottom: 1px solid #EDEDED;
      }
      .all-search{
        width:100%;
        height:40px;
        font-size:28px;
        font-weight:500;
        color:rgba(84,110,254,1);
        line-height:40px;
        padding-left: 17px;
      }
      .person-list{
        overflow:hidden;
        width: 100%;
        position: absolute;
        top: 0px;
        bottom: 0;
        border-bottom: 1px solid #fff;
        .result-all{
          width: 91%;
          height: 100%;
          overflow-y: scroll;
          overflow-x: hidden;
          margin:0 auto;
        }
      }
      .person-list-top{
          top: 0!important;
        }
      .search-list{
                display: flex;
                align-items: center;
                padding: 15px 0;
                border-bottom:1px solid #F5F5F5;
                img{
                    width: 80px;
                    height: 80px;
                    border-radius: 50%;
                }
                .name-circle, .search-title{
                    width: 80px;
                    height: 80px;
                    display: block;
                    background:rgba(252,179,63,1);
                    border-radius: 50%;
                    text-align: center;
                    line-height: 80px;
                    font-size:28px;
                    color: #fff;
                    img{
                      width: 80px!important;
                      height: 80px!important;
                      border-radius: 50%;
                    }
                }
                .item-checked{
                  color: #A9ABA9;
                }
                .search-title{
                  background: #536dfe!important;
                  overflow: hidden;
                }
                .item-name{
                    color: #333333;
                    font-size: 28px;
                    overflow: hidden;
                    white-space: nowrap;
                    width: 76%;
                    text-overflow: ellipsis;
                    padding-left: 10px;
                    p{
                      width: 100%;
                      text-overflow: ellipsis;
                      overflow: hidden;
                      white-space: nowrap;
                    }
                }
                .item-souce{
                    color: #919090;
                    font-size: 24px;
                }
            }
      .searchBtn{
        display: flex;
        align-items: center;
        justify-content: center;
        position:absolute;
        bottom:0;
        left:0;
        right:0;
        height:80px;
        button{
          flex: 1;
          background: #f1f1f1;
        }
        button:last-child{
          background: #536dfe;
          color:#fff;
        }
      }
    }
  }
}
</style>
